package com.ohuang.launcher.hook;

import android.os.IBinder;

import java.lang.reflect.InvocationHandler;

public abstract class SystemServiceHook {

    private   IBinder iBinder;

    protected   abstract String getIInterfaceClass();

    protected  abstract String getServiceName();

    public synchronized  void hook(InvocationHandler invocationHandler) {
        if (iBinder != null) {
            return;
        }
        IBinder systemService = BinderInvocationStub.getSystemService(getServiceName());
        if (systemService != null) {
            iBinder = systemService;
        }
        BinderInvocationStub binderInvocationStub = new BinderInvocationStub(systemService, getIInterfaceClass()
                , invocationHandler);
        BinderInvocationStub.replaceSystemService(getServiceName(), binderInvocationStub);
    }

    public synchronized  void unHook() {
        if (iBinder != null) {
            BinderInvocationStub.replaceSystemService(getServiceName(), iBinder);
            iBinder = null;
        }
    }
}
